package com.wc.codeforces.思维.The_Trail;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2025/1/14 17:24
 * @description
 * https://codeforces.com/contest/2055/problem/C
 */
public class Main {
    /**
     * 思路：
     * 行列和为 0
     * 假设目标总和是 S
     * 那全部数字起来就是  S * n = S * m, n != m时候 S == 0
     *                                 n == m 时候 S == 0 也是恒成立
     *                                 所以让目标和为 0 即可
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 1010;
    static long[] col = new long[N], row = new long[N];
    static long[][] a = new long[N][N];
    static char[] s = new char[N];
    static int n, m;

    public static void main(String[] args) {
        int T = sc.nextInt();
        while (T-- > 0) {
            n = sc.nextInt();
            m = sc.nextInt();
            Arrays.fill(row, 1, n + 1, 0);
            Arrays.fill(col, 1, m + 1, 0);
            s = (" " + sc.next()).toCharArray();
            for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= m; j++) {
                    a[i][j] = sc.nextInt();
                    row[i] += a[i][j];
                    col[j] += a[i][j];
                }
            }

            long sum = 0;
            int x = 1, y = 1;

            for (int i = 1; i <= n + m - 2; i++) {
                if (s[i] == 'R') {
                    long num = sum - col[y];
                    a[x][y] = num;
                    row[x] += num;
                    col[y] += num;
                    y++;
                } else {
                    long num = sum - row[x];
                    a[x][y] = num;
                    row[x] += a[x][y];
                    col[y] += a[x][y];
                    x++;
                }
            }
            a[n][m] = sum - row[n];

            for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= m; j++) {
                    out.print(a[i][j] + " ");
                }
                out.println();
            }
        }
        out.flush();
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}

